/**
 * User: alesj
 * Date: 2005.6.8
 * Time: 11:20:35
 * 
 * (C) Genera Lynx d.o.o.
 */

package com.generalynx.ecos.page;

import java.text.MessageFormat;

import org.springframework.beans.factory.InitializingBean;

public abstract class LanguageAlphaPageScroll extends IndexPageScroll implements InitializingBean {

    protected char[] chars;
    private String alphaProperty;

    public LanguageAlphaPageScroll() {
        chars = createAvailableChars();
    }

    public LanguageAlphaPageScroll(String alphaProperty) {
        this();
        this.alphaProperty = alphaProperty;
    }

    protected abstract char[] createAvailableChars();

    public void afterPropertiesSet() throws Exception {
        if (getAlphaProperty() == null) {
            throw new IllegalArgumentException("AlphaProperty must be set!");
        }
    }

    public boolean isLimitable() {
        return false;
    }

    public boolean isUsePageScroll() {
        return true;
    }

    public boolean isLast() {
        return (currentIdentifier == (chars.length - 1));
    }

    public String doTaskbar() {
        StringBuffer buffer = new StringBuffer();
        for(int i = 0; i < chars.length; i++) {
            buffer.append(MessageFormat.format(i == currentIdentifier ?
                                               BOLD_PATTERN :
                                               LINK_PATTERN,
                                               new Object[]{new Character(chars[i]), new Integer(i)})
            ).append(" ");
        }
        return buffer.toString();
    }

    public String query() {
        return select() + "from " + from() + " e" +
                " where upper(substring(e." + getAlphaProperty() + ", 1, 1)) >= :fA" +
                (!isLast() ? " and upper(substring(e." + getAlphaProperty() + ", 1, 1)) < :fB" : "");
    }

    protected  char beyondLastChar() {
        return (char)(chars[chars.length - 1] + 1);
    }

    public Object propertiesBean() {
        return isLast() ?
                new PropertyBean(Character.toUpperCase(chars[currentIdentifier])) :
                new PropertyBean(Character.toUpperCase(chars[currentIdentifier]),
                                 Character.toUpperCase(chars[currentIdentifier + 1]));
    }

    public String orderBy() {
        return getAlphaProperty();
    }

    private class PropertyBean {
        private char fA;
        private char fB;

        public PropertyBean(char fA) {
            this.fA = fA;
        }

        public PropertyBean(char fA, char fB) {
            this(fA);
            this.fB = fB;
        }

        public String getfA() {
            return String.valueOf(fA);
        }

        public String getfB() {
            return String.valueOf(fB);
        }
    }

    public String getAlphaProperty() {
        return alphaProperty;
    }

    public void setAlphaProperty(String alphaProperty) {
        this.alphaProperty = alphaProperty;
    }

}
